
{include file="layout/css"}
<style>
    .auth-search-section {
        margin: 30px 0 60px;
        background-color: #fff;
        border-radius: 4px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        padding: 25px;
    }
    .auth-search-input {
        border-radius: 4px 0 0 4px;
        height: 40px;
        transition: all 0.3s;
        border-color: #e6e6e6;
    }
    .auth-search-input:focus {
        border-color: #1E9FFF;
        box-shadow: 0 0 0 2px rgba(30,159,255,0.1);
    }
    .auth-search-btn {
        height: 40px;
        margin-left: 5px;
        border-radius: 4px;
        padding: 0 20px;
        font-size: 14px;
        transition: all 0.3s;
    }
    .auth-search-btn:hover {
        opacity: 0.9;
        box-shadow: 0 2px 6px rgba(0,0,0,0.1);
    }
    .auth-download-btn {
        width: 100%;
        height: 40px;
        line-height: 40px;
        border-radius: 4px;
        transition: all 0.3s;
        font-weight: 500;
        border-color: #e2e2e2;
    }
    .auth-download-btn:hover {
        background-color: #f9f9f9;
        border-color: #d2d2d2;
    }
    .auth-download-btn i {
        margin-right: 5px;
        font-size: 16px;
        vertical-align: middle;
    }
    .auth-info-box {
        margin-top: 25px;
        padding: 20px;
        background-color: #f8f8f8;
        border: 1px solid #e6e6e6;
        border-radius: 4px;
        min-height: 120px;
        transition: all 0.3s;
    }
    .auth-info-placeholder {
        color: #999;
        text-align: center;
        padding: 30px 0;
        font-size: 14px;
    }
    .section-title {
        margin-bottom: 20px;
        font-size: 18px;
        font-weight: 500;
        color: #333;
        border-left: 4px solid #1E9FFF;
        padding-left: 10px;
        line-height: 1.2;
    }
    /* 确保输入组件内的元素保持在同一行 */
    .layui-input-group {
        display: flex !important;
        align-items: center;
    }
    .layui-input-group .layui-input {
        flex: 1;
    }
    .layui-input-group .layui-input-suffix {
        flex-shrink: 0;
    }
</style>
{include file="layout/header"}
<div class="layui-container">
    <div class="auth-search-section">
        <div class="section-title">授权查询</div>
        <div class="layui-row">
            <div class="layui-col-xs12">
                <div class="layui-input-group" s>
                    <input type="text" name="auth_code" class="layui-input auth-search-input" placeholder="请输入域名或授权码查询" />
                    <div class="layui-input-suffix">
                        <button class="layui-btn layui-btn-normal auth-search-btn">查询</button>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row" style="margin-top: 15px;">
            <div class="layui-col-xs12">
                <button id="download-btn" class="layui-btn layui-btn-primary auth-download-btn"><i class="layui-icon layui-icon-download-circle"></i> <span>点击下载安装包</span></button>
            </div>
        </div>
        <div class="auth-info-box">
            <!--这里是查询的授权信息-->
            <div class="auth-info-placeholder">输入域名或授权码后点击查询按钮查看授权信息</div>
        </div>
    </div>
</div>
{include file="layout/footer"}

<script>
layui.use(['jquery', 'layer'], function(){
    var $ = layui.jquery;
    var layer = layui.layer;
    
    // 绑定查询按钮点击事件
    $('.auth-search-btn').on('click', function() {
        // 获取输入的授权码或域名
        var authCode = $('input[name="auth_code"]').val();
        
        // 验证输入不能为空
        if (!authCode) {
            layer.msg('请输入域名或授权码', {icon: 2});
            return false;
        }
        
        // 显示加载层
        var loadIndex = layer.load(1, {
            shade: [0.1, '#fff']
        });
        
        // 发送AJAX请求
        $.ajax({
            url: '{:url("home/check_auth")}',  // 请根据实际接口路径调整
            type: 'post',
            data: {
                auth_code: authCode
            },
            dataType: 'json',
            success: function(res) {
                layer.close(loadIndex);
                
                if (res.status == 1) {
                    // 请求成功，显示授权信息
                    var authInfo = res.data;
                    var html = '';
                    
                    if (authInfo) {
                        html = '<div class="layui-row">';
                        html += '<div class="layui-col-xs12"><p><strong>授权状态：</strong>' + (authInfo.status == 1 ? '<span style="color:#5FB878">已授权</span>' : '<span style="color:#FF5722">未授权</span>') + '</p></div>';
                        if (authInfo.domain) {
                            html += '<div class="layui-col-xs12"><p><strong>授权域名：</strong>' + authInfo.domain + '</p></div>';
                        }
                        if (authInfo.auth_code) {
                            html += '<div class="layui-col-xs12"><p><strong>授权码：</strong>' + authInfo.auth_code + '</p></div>';
                        }
                        if (authInfo.name) {
                            html += '<div class="layui-col-xs12"><p><strong>授权名称：</strong>' + authInfo.name + '</p></div>';
                        }
                        if (authInfo.expire_time) {
                            html += '<div class="layui-col-xs12"><p><strong>到期时间：</strong>' + authInfo.expire_time + '</p></div>';
                        }
                        html += '</div>';
                    } else {
                        html = '<div class="auth-info-placeholder">未找到相关授权信息</div>';
                    }
                    
                    $('.auth-info-box').html(html);
                } else {
                    // 请求失败，显示错误信息
                    layer.msg(res.msg || '查询失败，请稍后重试', {icon: 2});
                    $('.auth-info-box').html('<div class="auth-info-placeholder">查询失败，请稍后重试</div>');
                }
            },
            error: function() {
                layer.close(loadIndex);
                layer.msg('网络错误，请稍后重试', {icon: 2});
                $('.auth-info-box').html('<div class="auth-info-placeholder">网络错误，请稍后重试</div>');
            }
        });
    });
    
    // 支持回车键触发查询
    $('input[name="auth_code"]').on('keydown', function(e) {
        if (e.keyCode == 13) {
            $('.auth-search-btn').trigger('click');
        }
    });
    
    // 绑定下载按钮点击事件
    $('#download-btn').on('click', function() {
        var $btn = $(this);
        var $btnText = $btn.find('span');
        var originalText = $btnText.text();
        
        // 显示加载状态
        $btnText.text('正在准备下载...');
        $btn.attr('disabled', true);
        
        // 发送AJAX请求获取下载链接
        $.ajax({
            url: '{:url("home/download")}',
            type: 'get',
            dataType: 'json',
            success: function(res) {
                if (res.status == 1 && res.data && res.data.download_url) {
                    // 创建一个隐藏的a标签并触发点击下载
                    var downloadLink = document.createElement('a');
                    downloadLink.href = res.data.download_url;
                    downloadLink.target = '_blank';
                    downloadLink.download = res.data.filename || '安装包.zip';
                    document.body.appendChild(downloadLink);
                    downloadLink.click();
                    document.body.removeChild(downloadLink);
                    
                    layer.msg('下载已开始', {icon: 1});
                } else {
                    // 下载链接获取失败，回退到直接跳转
                    layer.msg(res.msg || '获取下载链接失败，正在跳转...', {icon: 2});
                    setTimeout(function() {
                        window.location.href = '{:url("home/download")}';
                    }, 1500);
                }
            },
            error: function() {
                // 请求失败，回退到直接跳转
                layer.msg('网络错误，正在跳转到下载页面...', {icon: 2});
                setTimeout(function() {
                    window.location.href = '{:url("home/download")}';
                }, 1500);
            },
            complete: function() {
                // 恢复按钮状态
                $btnText.text(originalText);
                $btn.attr('disabled', false);
            }
        });
    });
});
</script>
</body>
</html>
